//用户列表
$(function(){
	/*function clearForm(){
		$("#userFormId").form("clear"); //清空表单
		$("#sexId").combobox('setValue','1'); //男
		$("#countryId").combobox('setValue','1'); //国
		$("#passwordId").val('123');
		$("#userListId").datagrid("reload"); //刷新
	}*/
	//关闭dialog
	$('#closeId').click(function(){
		$("#userDialogId").dialog("close");
		//var queryParams = $('#userListId').datagrid('options').queryParams; //在此基础可进行模糊查询
		//queryParams.name = '李四'; //如，依次添加属性
		$("#userListId").datagrid("reload"); //刷新
	});
	
	//编辑用户
	$("#saveId").click(function(){
		if($("#userFormId").form('validate')){
			$("#userFormId").form("submit", {
				type : 'post',
				url : $WEB_ROOT_PATH + '/w/wh001/03',
				success : function(data) {
					
				}
			});
			//$.postJson($WEB_ROOT_PATH+'/w/wh001/03',$("#userFormId").serializeJsonStr(),function(result){
				/*if(result){
					if($("#pkId").val()!=''){
						$.messager.alert('提示','操作成功',"",function(){
							$("#userDialogId").dialog("close");
							clearForm();
						});
					}else{
						$.messager.alert('提示','操作成功',"",function(){
							clearForm();
						});
					}
				}else{
					$.messager.alert('提示','操作失败',"",function(){
					});
				}*/
			//},'json');
		}
	});

    $('#userListId').datagrid('doCellTip', { //设置悬浮提示
        onlyShowInterrupt : true, //默认是false，即全部提示
        position : 'bottom',
        maxWidth : '300px',
        /*specialShowFields : [{
            field : 'user_state',
            showField : 'sex'
        },{
        	field:'sex',
        	showField:'sex'
        }特殊显示],*/
        tipStyler : {
            'backgroundColor' : '#FFFFCC',
            borderColor : 'black 1px soild',
            boxShadow : '1px 1px 3px #292929',
        },
        contentStyler:{
        	'font-size':'16px',
        	'font-family':'宋体'
        }
    });

   
});

/**
 * 用户添加dialog
 */
function openUserDialog(){
	$("#userDialogId").dialog({
		closed: false,
		closable: false,
		left:0,
		top:0,
		title:'添加用户',
		iconCls:'icon-save',
		onOpen:function(){
			openSelectAddress(); //载入联动地址
		}
	});
}

/**
 * 用户修改dialog
 */
function upUser(){
	var selRow = $("#userListId").datagrid("getSelections");// 返回选中多行
	if(selRow.length==0 || selRow.length>1){
		$.messager.alert("提示", "请选择一行数据！","warning");
		return;
	}
	var id = selRow[0].id; //用户主键
	$.post($WEB_ROOT_PATH+'/w/wh001/getOneUserById',{id:id},function(result){
		$("#userDialogId").dialog({
			closed: false,
			closable: false,
			left:0,
			top:0,
			title:'修改用户',
			iconCls:'icon-edit',
			onOpen:function(){ //采用查询单个用户返回值
				$("#userFormId").form("load",result); //载入表单
				openSelectedAddress(result.provinceId);
			}
		});
		
	},'json');
	
}
/**
 * 删除用户
 */
function delUser(){
	var selRow = $("#userListId").datagrid("getSelections");// 返回选中多行
	if(selRow.length==0){
		$.messager.alert("提示", "请至少选择一行数据！","warning");
		return;
	}
	var ids = [];
	for(var i = 0;i<selRow.length;i++){
		ids.push(selRow[i].id);
	}
	$.messager.confirm("提示", "您确定要删除选中的数据吗？", function(data){
		if(data){
			var url = $WEB_ROOT_PATH+'/w/wh001/delUser';
			$.post(url,{"ids" : ids},function(r){
				$.messager.alert('提示','操作成功',"info",function(){
					$("#userListId").datagrid("reload"); //刷新
				});
			});
		}
	});
	
}

/**
 * 数据查询
 */
function someWhere(){
	$("#userListId").datagrid('load',{
		login_name:$.trim($("#login_name").val()),
		nick_name:$.trim($("#nick_name").val()),
		name:$.trim($("#name").val())
	});

}

/*function tipAddress(val,row){ //默认提示
	var address = row.address.substring(0,6)+"..."; 
	return '<span title="'+val+'" class="easyui-tooltip">'+address+'</span>';
}*/

function go(val,row){ //设置启动停用
	if(row.user_state=="启用"){
		return "<a href='javascript:void(0)' title='设置禁用' class='easyui-tooltip' onclick=\"setUp('"+row.id+"','"+row.user_state+"')\">"+row.user_state+"</a>";
	}
	if(row.user_state=="禁用"){
		return "<a href='javascript:void(0)' title='撤销禁用' class='easyui-tooltip' onclick=\"setUp('"+row.id+"','"+row.user_state+"')\">"+row.user_state+"</a>";
	}
}

/**
 * 设置启用停用
 * @param id
 * @param value
 */
function setUp(id,value){
	var url = $WEB_ROOT_PATH+'/w/wh001/setUp';
	$.post(url,{id:id,user_state:value},function(result) {
		if(result){
			$.messager.alert('提示','操作成功',"info",function(){
				$("#userListId").datagrid("reload"); //刷新
			});
		}
	});
}

function goUser(val,row){ //用户删除，恢复
	if(row.final_state=="未删除"){
		return "<a href='javascript:void(0)' title='删除用户' class='easyui-tooltip' onclick=\"setUser('"+row.id+"','"+row.final_state+"')\">"+row.final_state+"</a>";
	}
	if(row.final_state=="已删除"){
		return "<a href='javascript:void(0)' title='撤销删除' class='easyui-tooltip' onclick=\"setUser('"+row.id+"','"+row.final_state+"')\">"+row.final_state+"</a>";
	}
}

/**
 * 用户删除，恢复
 * @param id
 * @param value
 */
function setUser(id,value){
	var url = $WEB_ROOT_PATH+'/w/wh001/setUser';
	$.post(url,{id:id,final_state:value},function(result) {
		if(result){
			$.messager.alert('提示','操作成功',"info",function(){
				$("#userListId").datagrid("reload"); //刷新
			});
		}
	});
}

/**
 * -----------------------------bottom---------------------
 * 打开地址
 */
function openSelectAddress(){ //打开地址
	var url = $WEB_ROOT_PATH+'/w/wh004/1';
	$.postJson(url, function(result) {
		$("#provinceId").combobox({
			data : result,
			valueField:'id',
			textField:'name',
			cache:false,
			editable:false, //只读
			/*value:result[0].id,*/ //设置默认选中项但是不触发下一步联动事件
			onLoadSuccess: function () { //加载完成后,设置选中第一项  
				$("#provinceId").combobox('select',result[0].id);
			},
			onSelect:function(record){
				var provinceId = record.id;
				var url = $WEB_ROOT_PATH+'/w/wh005/1';
				$.post(url,{provinceId:provinceId}, function(result) { //二级联动
					$("#cityId").combobox({
						data : result,
						valueField:'id',
						textField:'name',
						cache:false,
						editable:false,
						onLoadSuccess: function () { //加载完成后,设置选中第一项  
							$("#cityId").combobox('select',result[0].id);
						},
						onSelect:function(record){
							var cityId = record.id;
							var url = $WEB_ROOT_PATH+'/w/wh006/1';
							$.post(url,{cityId:cityId}, function(result) { //三级联动
								$("#districtId").combobox({
									data : result,
									valueField:'id',
									textField:'name',
									cache:false,
									editable:false,
									onLoadSuccess: function () { //加载完成后,设置选中第一项  
										$("#districtId").combobox('select',result[0].id);
									}
								});
							},'json');
						}
					});
				},'json');
			}
		});
	},'json');
}
/**
 * 回显地址
 * @param provinceId
 */
function openSelectedAddress(provinceId){ //回显地址
	var url = $WEB_ROOT_PATH+'/w/wh010/getSysProvincesByCountryId';
	$.postJson(url, function(result) {
		$("#provinceId").combobox({
			data : result,
			valueField:'id',
			textField:'name',
			cache:false,
			editable:false, //只读
			onLoadSuccess: function () { //加载完成后,设置选中第一项
				for (var i = 0;i<result.length;i++) { //主要区别
                    if (result[i].id == provinceId) {
                        $(this).combobox("select",provinceId);
                    }
                } 
			},
			onSelect:function(record){
				var provinceId = record.id;
				var url = $WEB_ROOT_PATH+'/w/wh010/getSysCityByProvinceId';
				$.post(url,{provinceId:provinceId}, function(result) { //二级联动
					$("#cityId").combobox({
						data : result,
						valueField:'id',
						textField:'name',
						cache:false,
						editable:false,
						onLoadSuccess: function () { //加载完成后,设置选中第一项  
							$("#cityId").combobox('select',result[0].id);
						},
						onSelect:function(record){
							var cityId = record.id;
							var url = $WEB_ROOT_PATH+'/w/wh010/getSysDistrictBycityId';
							$.post(url,{cityId:cityId}, function(result) { //三级联动
								$("#districtId").combobox({
									data : result,
									valueField:'id',
									textField:'name',
									cache:false,
									editable:false,
									onLoadSuccess: function () { //加载完成后,设置选中第一项  
										$("#districtId").combobox('select',result[0].id);
									}
								});
							},'json');
						}
					});
				},'json');
			}
		});
	},'json');
}
